import SourceData from '../source_data.js';

Page({
  data: {
    background: ['blue', 'red', 'yellow'],
    indicatorDots: true,
    autoplay: false,
    vertical: false,
    interval: 1000,
    circular: false,
    pageData: [],

    testParam: '',
    goodInfoList: SourceData.goodsInfoListData,
  },
  onLoad() {
    console.log("goodInfoList")
    console.log(this.data.goodInfoList)


    var groupNum = Math.ceil(this.data.goodInfoList.length / 4);
    var temp = [];
    for (var i = 0; i < groupNum; i++) {
      var tempList = [];
      if (i * 4 + 0 < this.data.goodInfoList.length)
        tempList.push(this.data.goodInfoList[i * 4 + 0]);
      if (i * 4 + 1 < this.data.goodInfoList.length)
        tempList.push(this.data.goodInfoList[i * 4 + 1]);
      if (i * 4 + 2 < this.data.goodInfoList.length)
        tempList.push(this.data.goodInfoList[i * 4 + 2]);
      if (i * 4 + 3 < this.data.goodInfoList.length)
        tempList.push(this.data.goodInfoList[i * 4 + 3]);

      temp.push(tempList);
    }

    this.setData({
      pageData: temp
    });
  },

  method: {
    printDebugInfo() {
      console.log("printDebugInfo goodInfoList = ")
      console.log(this.props.goodInfoList)

      console.log("printDebugInfo testParam = " + this.props.testParam)

      console.log("printDebugInfo goodInfoList = " + this.props.goodInfoList.length)

    },

    onAddCartClick(e) {
      var goodId = e.target.dataset.value.goodId;
      this.onAddToCart(goodId, null)
    },

    onItemAddCartClick() {
      var goodId = e.target.dataset.value.goodId;
      this.onAddToCart(goodId, null)
    },

    onAddToCart(id, exts) {
      my.tb.addToCart({
        itemIds: id,
        exts: exts ? exts : '',
        success: (res) => {
          my.alert({ content: "success" + JSON.stringify(res) })
        },
        fail: (res) => {
          my.alert({ content: "fail" + JSON.stringify(res) })
          console.log(res)
        },
      })
    }
  }
});
